import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/layouts/MainLayout.vue'

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表盘', icon: 'Odometer' },
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/profile/Profile.vue'),
        meta: { title: '个人中心', icon: 'User' },
      },
    ],
  },
]

// 演示路由
export const demoRoutes: RouteRecordRaw[] = [
  {
    path: '/demo',
    component: Layout,
    meta: { title: '演示功能', icon: 'Example' },
    children: [
      {
        path: '/demo/rich-text',
        name: 'RichTextDemo',
        component: () => import('@/views/demo/RichTextDemo.vue'),
        meta: { title: '富文本编辑器', icon: 'Edit' },
      },
      {
        path: '/demo/json-editor',
        name: 'JsonEditorDemo',
        component: () => import('@/views/demo/JsonEditorDemo.vue'),
        meta: { title: 'JSON编辑器', icon: 'Document' },
      },
      {
        path: '/demo/code-editor',
        name: 'CodeEditorDemo',
        component: () => import('@/views/demo/CodeEditorDemo.vue'),
        meta: { title: '代码编辑器', icon: 'Code' },
      },
    ],
  },
]

// 系统管理路由
export const systemRoutes: RouteRecordRaw[] = [
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: '/system/user',
        name: 'SystemUser',
        component: () => import('@/views/system/User.vue'),
        meta: { title: '用户管理', icon: 'User' },
      },
      {
        path: '/system/role',
        name: 'SystemRole',
        component: () => import('@/views/system/Role.vue'),
        meta: { title: '角色管理', icon: 'Lock' },
      },
      {
        path: '/system/dept',
        name: 'SystemDept',
        component: () => import('@/views/system/Dept.vue'),
        meta: { title: '部门管理', icon: 'OfficeBuilding' },
      },
      {
        path: '/system/menu',
        name: 'SystemMenu',
        component: () => import('@/views/system/Menu.vue'),
        meta: { title: '菜单管理', icon: 'Menu' },
      },
      {
        path: '/system/dict',
        name: 'SystemDict',
        component: () => import('@/views/system/Dict.vue'),
        meta: { title: '字典管理', icon: 'Collection' },
      },
      {
        path: '/system/log',
        name: 'SystemLog',
        component: () => import('@/views/system/Log.vue'),
        meta: { title: '系统日志', icon: 'Document' },
      },
      {
        path: '/system/config',
        name: 'SystemConfig',
        component: () => import('@/views/system/Config.vue'),
        meta: { title: '系统配置', icon: 'Tools' },
      },
      {
        path: '/system/job',
        name: 'SystemJob',
        component: () => import('@/views/system/Job.vue'),
        meta: { title: '定时任务', icon: 'Clock' },
      },
    ],
  },
]

// 错误页面路由
export const errorRoutes: RouteRecordRaw[] = [
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/errors/404.vue'),
    meta: { title: '页面未找到' },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/errors/403.vue'),
    meta: { title: '权限不足' },
  },
  {
    path: '/500',
    name: '500',
    component: () => import('@/views/errors/500.vue'),
    meta: { title: '服务器错误' },
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/errors/404.vue'),
  },
]

// 认证路由
export const authRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: { title: '登录' },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/auth/Register.vue'),
    meta: { title: '注册' },
  },
]

// 系统设计路由
export const devRoutes: RouteRecordRaw[] = [
  {
    path: '/dev',
    component: Layout,
    meta: { title: '系统设计', icon: 'Operation' },
    children: [
      {
        path: '/dev/module',
        name: 'DevModule',
        component: () => import('@/views/dev/Module.vue'),
        meta: { title: '模块管理', icon: 'Document' },
      },
    ],
  },
]

// 核心路由
export const coreRoutes: RouteRecordRaw[] = [
  {
    path: '/core',
    component: Layout,
    meta: { title: '核心功能', icon: 'Aim' },
    children: [
      {
        path: '/core/list',
        name: 'CoreList',
        component: () => import('@/views/core/List.vue'),
        meta: { title: '列表管理', icon: 'List' },
      },
      {
        path: '/core/tree',
        name: 'CoreTree',
        component: () => import('@/views/core/Tree.vue'),
        meta: { title: '树状管理', icon: 'Tickets' },
      },
    ],
  },
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes: [
    ...constantRoutes,
    ...demoRoutes,
    ...errorRoutes,
    ...authRoutes,
    ...systemRoutes,
    ...devRoutes,
    ...coreRoutes,
  ],
})

export default router
